<template>
  <div class="app-container">
    <div class="syl-group">
      <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
        <el-form-item label="活动名称:" prop="companyName">
          <el-input
            v-model="queryParams.companyName"
            placeholder="请输入"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="活动时间:">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            size="small"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="活动状态:">
          <el-radio-group v-model="queryParams.radio" class="syl-group-order" @change="getGroup">
            <el-radio :label="1">全部活动</el-radio>
            <el-radio :label="2">发布中</el-radio>
            <el-radio :label="3">未发布</el-radio>
            <el-radio :label="4">已结束</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" class="ordinary" style="margin-right: 15px" @click="resetQuery">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新建
          </el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="equWristbandList"
        :header-cell-style="{background:'#F3F9FF',color:'#333333',fontWeight: 600}"
      >
        <el-table-column label="活动主题" align="center" prop="order.orderNo" :show-overflow-tooltip="true" />
        <el-table-column label="开始时间" align="center" prop="order.createTime" :show-overflow-tooltip="true" />
        <el-table-column label="结束时间" align="center" prop="order.companyName" :show-overflow-tooltip="true" />
        <el-table-column label="发起人" align="center" prop="order.contactPhone" />
        <el-table-column label="活动状态" align="center" prop="order.packageNum" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <router-link :to="{ path: '/alfalfa/into',query:{id:1} }">
              <el-button
                size="mini"
                type="text"
              >查看
              </el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改门店对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="true"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model.trim="form.contactName" placeholder="请输入负责人" :maxlength="16" />
        </el-form-item>
        <el-form-item label="活动封页" prop="environment">
          <el-upload
            ref="uploadMent"
            :class="[hideUpload===1?'hide':'']"
            :action="upload.url"
            :headers="upload.headers"
            :disabled="upload.isUploading"
            list-type="picture-card"
            :file-list="imageData"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove1"
            :before-upload="beforeAvatarUpload"
            :limit="limitCount"
            :on-success="handleFaceEnvironment"
            :on-exceed="handleAvatarData"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="活动类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio label="H5" :value="1" />
            <el-radio label="内部跳转" :value="2" />
            <el-radio label="图片" :value="3" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动链接" prop="link">
          <el-input v-model.trim="form.link" placeholder="请输入负责人" :maxlength="100" />
        </el-form-item>
        <el-form-item label="活动描述" prop="describe">
          <el-input
            v-model="form.describe"
            type="textarea"
            autosize
            :maxlength="500"
            resize="none"
          />
        </el-form-item>
        <el-form-item label="活动发布" prop="release">
          <el-radio-group v-model="form.release">
            <el-radio label="立即发布" :value="1" />
            <el-radio label="定时发布" :value="2" />
            <el-radio label="暂不发布" :value="3" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH-mm-ss"
            placeholder="选择日期时间"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH-mm-ss"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-divider direction="vertical" />
        <el-button v-preventReClick type="primary" :loading="loadingValue" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import serverMap from '../../../../config/serverMap'
import { getToken } from '@/utils/auth'
import * as imageConversion from 'image-conversion'

export default {
  name: 'Index',
  data() {
    return {
      // 活动
      hideUpload: null,
      // 活动封页
      imageData: [],
      // 加载动画
      loadingValue: false,
      // 活动时间
      dateRange: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 2,
      // 活动列表格数据
      equWristbandList: [{}],
      // 状态
      options: [],
      // 最大上传数
      limitCount: 1,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        // 订单金额
        radio: 1
      },
      // 表单参数
      form: {
        contactName: null,
        coverPage: null,
        type: null,
        link: null,
        describe: null,
        release: null,
        startTime: null,
        endTime: null
      },
      // 表单校验
      rules: {},
      // 活动状态
      valueStatus: [
        { value: 1, label: '全部活动' },
        { value: 2, label: '发布中' },
        { value: 3, label: '未发布' },
        { value: 4, label: '已结束' }
      ],
      // 用户导入参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: serverMap.base + '/api/sys/files/upload'
      }
    }
  },
  methods: {
    /** 活动中心列表 **/
    getList() {

    },
    /** 发布 **/
    getGroup() {

    },
    /** 查询 **/
    handleQuery() {
      this.queryParams.pageNum = 1
      this.queryParams.radio = 1
      this.getList()
    },
    /** 重置 **/
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    /** 新建活动 **/
    handleAdd() {
      this.title = '新建活动'
      this.open = true
      this.reset()
    },
    /** 表单重置 **/
    reset() {
      this.form = {
        contactName: null,
        coverPage: null,
        type: null,
        link: null,
        describe: null,
        release: null,
        startTime: null,
        endTime: null
      }
      this.resetForm('form')
    },
    /** 取消 **/
    cancel() {
      this.open = false
    },
    /** 提交 **/
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.msgSuccess('添加成功')
          this.open = false
        }
      })
    },
    /** 点击文件列表中已上传的文件时的钩子 **/
    handlePictureCardPreview(file) {

    },
    // 超出事件
    handleAvatarData() {
      this.msgInfo('图片上传次数最多为3张!')
    },
    /** 门店删除 **/
    handleRemove1(file, fileList) {
      this.form.coverPage = fileList.map((item) => item.url)
      this.hideUpload = fileList.length >= this.limitCount
    },
    /** 图片上传格式判断 */
    beforeAvatarUpload(file) {
      new Promise((resolve, reject) => {
        const isJPG = file.type.split('/')[1]
        const value = ['jpeg', 'png', 'jpg', 'bmp', 'gif', 'JPEG', 'PNG', 'JPG', 'BMP']
        const name = value.includes(isJPG)
        if (!name) {
          this.msgError('上传图片格式为' + 'jpeg', 'png', 'jpg', 'bmp', 'gif', 'JPEG', 'PNG', 'JPG', 'BMP')
        }
        const isLt2M = file.size / 1024 / 1024 < 2
        const _URL = window.URL || window.webkitURL
        // 这里需要计算出图片的长宽
        const img = new Image()
        img.onload = function() {
          file.width = img.width // 获取到width放在了file属性上
          file.height = img.height // 获取到height放在了file属性上
          const valid = img.width > 2000 // 图片宽度大于2000
          // 这里我只判断了图片的宽度,compressAccurately有多个参数时传入对象
          if (valid || isLt2M) {
            imageConversion.compressAccurately(file, {
              size: 400,
              width: 2000
            }).then(res => { // console.log(33, res)
              resolve(res)
            })
          } else {
            resolve(file)
          }
        } // 需要把图片赋值
        img.src = _URL.createObjectURL(file)
        if (!isLt2M) {
          this.msgError('上传头像图片大小不能超过 2MB,请重新上传!')
        }
        return name && isLt2M
      })
    },
    /** 门店环境上传成功 **/
    handleFaceEnvironment(res, file, fileList) {
      if (res.code === 0) {
        const { data } = res
        this.form.coverPage.push(data.url)
        this.hideUpload = fileList.length >= this.limitCount
      }
    }
  }

}
</script>
<style lang="scss" scoped>
.hide {
  /deep/ .el-upload--picture-card {
    display: none;
  }
}

.mb8 {
  margin: 10px 0;
}
</style>
